﻿using HandyControl.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using YY.Admin.ViewModels;
using Window = HandyControl.Controls.Window;

namespace YY.Admin.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //this.Closed += MainWindow_Closed;
            ContentScrollViewer.SizeChanged += OnScrollViewerSizeChanged;
        }

        private void MainWindow_Closed(object? sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void UserMenuButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.ContextMenu != null)
            {
                button.ContextMenu.PlacementTarget = button;
                button.ContextMenu.IsOpen = true;
            }
        }

        private void Splitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            double newWidth = LeftMenuTree.Width + e.HorizontalChange;

            // 获取主窗口宽度
            double windowWidth = this.ActualWidth;

            // 最小宽度
            double minWidth = 50;

            // 最大宽度
            double maxWidth = windowWidth - LeftSidebar.Width - GridSplitter.Width - minWidth;

            if (newWidth < minWidth)
                newWidth = minWidth;
            else if (newWidth > maxWidth)
                newWidth = maxWidth;

            LeftMenuTree.Width = newWidth;
        }

        private void TabControl_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 查找点击点是否在 TabItem Header 上
            var dep = e.OriginalSource as DependencyObject;

            while (dep != null && dep is not TabItem)
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            // 如果右键点在 TabItem 上
            if (dep is TabItem)
            {
                // 阻止 TabControl 切换 SelectedItem
                e.Handled = true; 
            }
        }

        private void OnScrollViewerSizeChanged(object sender, SizeChangedEventArgs e)
        {
            // 检查是否需要显示滚动条
            //bool needsScroll = ContentPanel.ActualHeight > ContentScrollViewer.ActualHeight;

            //bool needsScroll = ContentScrollViewer.ScrollableHeight > 0;

            bool needsScroll = ContentScrollViewer.ComputedVerticalScrollBarVisibility == Visibility.Visible;

            if (needsScroll)
            {
                // 有滚动条：显示固定按钮，隐藏内部按钮
                FixedButton.Visibility = Visibility.Visible;
                InnerButton.Visibility = Visibility.Collapsed;
            }
            else
            {
                // 无滚动条：隐藏固定按钮，显示内部按钮
                FixedButton.Visibility = Visibility.Collapsed;
                InnerButton.Visibility = Visibility.Visible;
            }
        }

        private void OnSkinTypeChanged(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button { Tag: SkinType skinType })
            {
                var vm = DataContext as MainWindowViewModel;
                vm?.AppSettingsViewModel?.SkinType = skinType;
            }
        }
    }
}